package ${baseInfo.basePackage!''}.controller.${baseInfo.moduleName!''};

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ct.common.controller.BaseController;
import ${wholeClassName!''};
import ${baseInfo.basePackage!''}.service.${baseInfo.moduleName!''}.${baseInfo.className!''}Service;
import com.ct.common.controller.ObjectExcelView;
import com.ct.admin.domain.dict.SmDict;
import com.ct.admin.service.dict.SmDictService;
import com.ct.common.annotation.AppRes;
import com.ct.common.dto.ReturnObj;
import com.ct.common.dto.PageDto;

@Controller
@RequestMapping(value="/${baseInfo.moduleName!''}/${baseInfo.objectName!''}")
public class ${baseInfo.className!''}Controller extends BaseController{

	private static Logger logger = LoggerFactory.getLogger(${baseInfo.className!''}Controller.class);
	
	@Autowired
	private ${baseInfo.className!''}Service ${baseInfo.objectName!''}Service;
	
	
	@Autowired
	private SmDictService smDictService;
	
	@RequestMapping(value="/take/{id}")
	public @ResponseBody ReturnObj find(@PathVariable("id") Long id,
			HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		ReturnObj returnObj = this.getSucReturnObj();	
		HttpServletRequest request = this.getRequest(requestOld);
		${baseInfo.className!''} ${baseInfo.objectName!''} = this.${baseInfo.objectName!''}Service.getById(id);
		
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("${baseInfo.objectName!''}",${baseInfo.objectName!''});
		returnObj.setDataMap(dataMap);
		return returnObj;
	}
	
	@RequestMapping(value="/page/listPagination")
	public ModelAndView listPage(PageDto page,HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		HttpServletRequest request = this.getRequest(requestOld);
		ModelAndView mv = this.getModelAndView();
		Map<String,Object> paramsMap = this.getParamsMap(request, true);
		<#list baseInfo.listSelectPropertyInfo as pi>
		//${pi.remarks!''}
		List<SmDict> listSmDict${pi.propertyName?cap_first} = this.smDictService.listSubDictByParent("${pi.dictParentCode!''}");
		mv.addObject("listSmDict${pi.propertyName?cap_first}", listSmDict${pi.propertyName?cap_first});
		</#list>
		PageDto	pageDto = this.${baseInfo.objectName!''}Service.selectByPage(page,paramsMap);	//列出Dictionaries列表
		mv.setViewName("system/${baseInfo.moduleName!''}/${baseInfo.objectName!''}_list");
		mv.addObject("paramsMap", paramsMap);
		mv.addObject("pageDto", pageDto);
		return mv;
	}
	
	/**查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goView")
	public ModelAndView goView(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		HttpServletRequest request = this.getRequest(requestOld);
		ModelAndView mv = this.getModelAndView();
		${baseInfo.objectName!''} =  this.${baseInfo.objectName!''}Service.getById(${baseInfo.objectName!''}.get${baseInfo.primaryKeyProperty?cap_first}());					//根据ID读取
		mv.setViewName("system/${baseInfo.moduleName!''}/${baseInfo.objectName!''}_view");
		mv.addObject("${baseInfo.objectName!''}", ${baseInfo.objectName!''});
		return mv;
	}
	
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		HttpServletRequest request = this.getRequest(requestOld);
		ModelAndView mv = this.getModelAndView();
		<#list baseInfo.listSelectPropertyInfo as pi>
		//${pi.remarks!''}
		List<SmDict> listSmDict${pi.propertyName?cap_first} = this.smDictService.listSubDictByParent("${pi.dictParentCode!''}");
		mv.addObject("listSmDict${pi.propertyName?cap_first}", listSmDict${pi.propertyName?cap_first});
		</#list>
		mv.setViewName("system/${baseInfo.moduleName!''}/${baseInfo.objectName!''}_edit");
		mv.addObject("${baseInfo.objectName!''}", ${baseInfo.objectName!''});
		mv.addObject("action", "add");
		return mv;
	}
	
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		HttpServletRequest request = this.getRequest(requestOld);
		ModelAndView mv = this.getModelAndView();
		${baseInfo.objectName!''} =  this.${baseInfo.objectName!''}Service.getById(${baseInfo.objectName!''}.get${baseInfo.primaryKeyProperty?cap_first}());					//根据ID读取
		<#list baseInfo.listSelectPropertyInfo as pi>
		//${pi.remarks!''}
		List<SmDict> listSmDict${pi.propertyName?cap_first} = this.smDictService.listSubDictByParent("${pi.dictParentCode!''}");
		mv.addObject("listSmDict${pi.propertyName?cap_first}", listSmDict${pi.propertyName?cap_first});
		</#list>
		mv.setViewName("system/${baseInfo.moduleName!''}/${baseInfo.objectName!''}_edit");
		mv.addObject("${baseInfo.objectName!''}", ${baseInfo.objectName!''});
		mv.addObject("action", "edit");
		return mv;
	}
	
	@RequestMapping(value="/add")
	@AppRes(log=true,resCode="add${baseInfo.className!''}",resName="新增${baseInfo.tableNameRemark!''}",parentResCode="${baseInfo.objectName!''}")
	public @ResponseBody ReturnObj  add(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		ReturnObj returnObj = this.getSucReturnObj();	
		HttpServletRequest request = this.getRequest(requestOld);
		//业务逻辑判断 与属性赋值
		
		this.${baseInfo.objectName!''}Service.save(${baseInfo.objectName!''});
		Map<String,Object> dataMap = new HashMap<String,Object>();
		
		returnObj.setDataMap(dataMap);
		return returnObj;
	}
	
	
	@RequestMapping(value="edit")
	@AppRes(log=true,resCode="edit${baseInfo.className!''}",resName="编辑${baseInfo.tableNameRemark!''}",parentResCode="${baseInfo.objectName!''}")
	public @ResponseBody ReturnObj  edit(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		ReturnObj returnObj = this.getSucReturnObj();	
		HttpServletRequest request = this.getRequest(requestOld);
		//业务逻辑判断 与属性赋值
		
		this.${baseInfo.objectName!''}Service.updateById(${baseInfo.objectName!''});
		Map<String,Object> dataMap = new HashMap<String,Object>();
		
		returnObj.setDataMap(dataMap);
		return returnObj;
	}
	
	@RequestMapping(value="/hasExists")
	public @ResponseBody ReturnObj  hasExists(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		ReturnObj returnObj = this.getSucReturnObj();	
		HttpServletRequest request = this.getRequest(requestOld);
		
	    int count = this.${baseInfo.objectName!''}Service.count(${baseInfo.objectName!''});
	    if(count > 0){
	    	return this.getFailReturnObj("${baseInfo.tableNameRemark!''}已存在!");
	    }
		
		Map<String,Object> dataMap = new HashMap<String,Object>();
		returnObj.setDataMap(dataMap);
		return returnObj;
	}
	
	
	@RequestMapping(value="deleteIds")
	@AppRes(log=true,resCode="delete${baseInfo.className!''}",resName="删除${baseInfo.tableNameRemark!''}",parentResCode="${baseInfo.objectName!''}")
	public @ResponseBody ReturnObj  deleteIds(String ids,HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		ReturnObj returnObj = this.getSucReturnObj();	
		HttpServletRequest request = this.getRequest(requestOld);
		this.${baseInfo.objectName!''}Service.deleteIds(ids);
		Map<String,Object> dataMap = new HashMap<String,Object>();
		returnObj.setDataMap(dataMap);
		return returnObj;
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/excel")
	@AppRes(log=true,resCode="excel${baseInfo.className!''}",resName="导出${baseInfo.tableNameRemark!''}",parentResCode="${baseInfo.objectName!''}")
	public ModelAndView excel(${baseInfo.className!''} ${baseInfo.objectName!''},HttpServletRequest requestOld, HttpServletResponse response) throws Exception{
		HttpServletRequest request = this.getRequest(requestOld);
		ModelAndView mv = this.getModelAndView();
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<String> titles = new ArrayList<String>();
		<#list baseInfo.propertyInfos as pi>
		titles.add("${pi.remarks!''}"); 		//${pi_index+1}
		</#list>
		<#list baseInfo.listSelectPropertyInfo as pi>
		titles.add("${pi.remarks!''}"); 		//${pi_index+1}
		</#list>
		dataMap.put("titles", titles);
		Map<String,Object> paramsMap = this.getParamsMap(request, true);
		List<${baseInfo.className!''}> queryList = this.${baseInfo.objectName!''}Service.selectPageAll(paramsMap);
		List<Map> valueList = new ArrayList<Map>();
		for(${baseInfo.className!''} temp : queryList){
			Map vpd = new HashMap<String,Object>();
			int j = 1;
			<#list baseInfo.propertyInfos as pi>
			vpd.put(ObjectExcelView.prefix + j++, temp.get${pi.propertyName?cap_first}());		//${pi_index+1}
			</#list>
			<#list baseInfo.listSelectPropertyInfo as pi>
			vpd.put(ObjectExcelView.prefix + j++, temp.get${pi.propertyName?cap_first}Name());		//${pi_index+1}
			</#list>
			valueList.add(vpd);
		}
		dataMap.put("valueList", valueList);
		dataMap.put("fileName", "${baseInfo.objectName!''}");
		ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}
}
